package com.kaifamiao.controller;

import com.kaifamiao.entity.Customer;
import com.kaifamiao.service.CustomerService;
import com.kaifamiao.token.JsonWebTokenHelper;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;
import java.util.Map;
import java.util.logging.Logger;

@RestController
@RequestMapping(path = "/customer/**",
                method = { RequestMethod.GET,RequestMethod.POST,RequestMethod.PUT,RequestMethod.DELETE})
public class CustomerController {

    @Autowired
    private Logger logger;

    @Autowired
    private JsonWebTokenHelper tokenHelper;

    private CustomerService customerService;

    @PostMapping("/register")
    public Map<String,Object> register( @RequestBody Customer c ){
        logger.info( "注册" );
        // 检查邮箱或手机号是否被占用
        boolean exists = customerService.exists( c.getEmail(), c.getTel() );
        if( exists ) {
            return Map.of("success", false, "message", "邮箱或手机号被占用");
        }
        customerService.save( c );
        return Map.of("success", true, "message", "注册成功");
    }

    @PostMapping("/login")
    public Map<String,Object> login( @RequestBody Customer c ) {
        logger.info("登录" );
        Customer x = customerService.find( c.getEmail(), c.getTel() );
        if( x == null ) {
            return Map.of( "success", false, "message", "邮箱或手机号错误");
        }

        if( !x.getPassword().equals( c.getPassword() ) ) {
            return Map.of( "success", false, "message", "登录密码错误");
        }

        String token = tokenHelper.generate( Map.of("id", x.getId(), "email", x.getEmail() ) );
        return Map.of( "success", true, "message", "登录成功" , "token", token );
    }

    @GetMapping("/find/all")
    public List<Customer> findAll() {
        logger.info( "查询所有用户" );
        return customerService.findAll();
    }

    @GetMapping("/find")
    public Customer find( @RequestAttribute("customerId") Integer customerId ) {
        logger.info( "查询当前用户" );
        return customerService.find(customerId);
    }

    @DeleteMapping("/remove/{id}")
    public Map<String,Object> remove( @PathVariable("id") Integer id ) {
        logger.info( "删除用户" );
        if( customerService.remove(id) ) {
            return Map.of( "success", true, "message", "删除成功" );
        }
        return Map.of("success", false, "message", "删除失败" );
    }

    @PutMapping("/update")
    public Map<String,Object> update( @RequestBody Customer c ) {
        logger.info("修改用户");
        logger.info( "married: " + c.isMarried() );
        if( customerService.update(c) ) {
            return Map.of( "success", true, "message", "修改成功");
        }
        return Map.of( "success", false, "message", "修改失败");
    }

    public CustomerService getCustomerService() {
        return customerService;
    }

    @Autowired
    public void setCustomerService(CustomerService customerService) {
        this.customerService = customerService;
    }
}
